package com.mrzhou.web.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.mrzhou.core.tool.utils.LocalDateUtil;
import com.mrzhou.web.props.CloudWebProperties;
import com.mrzhou.web.props.HttpClientPoolProperties;
import com.mrzhou.web.resolver.TokenArgumentResolver;
import com.mrzhou.web.secure.aspect.AuthAspect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author qiang.zhou
 */
@Slf4j
@Configuration
public class CloudWebConfiguration implements WebMvcConfigurer {

    /** ========== 当前模块使用的bean配置 Start ========== */

    @Bean
    public AuthAspect authAspect() {
        return new AuthAspect();
    }

    @Bean
    public HttpClientPoolProperties httpClientPoolProperties() {
        return new HttpClientPoolProperties();
    }

    @Bean
    public CloudWebProperties cloudWebProperties() {
        return new CloudWebProperties();
    }

    /**
     * 统一处理项目中对象序列化及反序列化的方式
     *  其他方式的序列化, 请引用此Bean, 方便统一管理
     * @return
     */
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper om = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        //序列化
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(LocalDateUtil.DATE_TIME_FORMAT)));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(LocalDateUtil.DATE_FORMAT)));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(LocalDateUtil.TIME_FORMAT)));
        //反序列化
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(LocalDateUtil.DATE_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(LocalDateUtil.DATE_FORMAT)));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(LocalDateUtil.TIME_FORMAT)));

        SimpleModule simpleModule = new SimpleModule();
        // Long类型使用String序列化， 防止数据出现精度丢失的问题
        simpleModule.addSerializer(Long.class, new ToStringSerializer());
        // Integer类型使用String序列化, 方便前端处理
        simpleModule.addSerializer(Integer.class, new ToStringSerializer());

        // 在解析时忽略json串中未提供set的属性名称
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        om.registerModule(javaTimeModule);
        om.registerModule(simpleModule);
        return om;
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        log.info("载入自动解析token中的参数类！");
        resolvers.add(new TokenArgumentResolver());
    }

    /*@Bean
    @ConditionalOnBean(DispatcherServlet.class)
    public InitDispatcherServlet initDispatcherServlet() {
        return new InitDispatcherServlet();
    }*/


    /** ========== 当前模块使用的bean配置 END ========== */



}
